var ns;
app = new Vue({
    el: '#app',
    data: {
        headerlist: [],
        ProjectList: [],//公司列表---总公司和子公司
        CompanyID: 0,//公司ID
        FullName: '',//公司名称
        defaultImg: "../image/default.png",//默认头像
        isshow: false,
        childlist: [],
        currentchosenlist: [],//部门列表
        form: {
            keywords: '',
        },
        list:[],//列表
        topHeader: 85,
        ID: 0,
        PublishType: 1,//1-派单  2-重派
        ServiceType: 1,//1-维修工单
        IsMultiple: false,//是否是多选
        IsChoose:false,//是否只是选择
        chooseList: [],
        showchoose: false,
        showsearch: false,
        select_id_list:[],
    },
    methods: {
        get_project: function () {
            var that = this;
            that.form.keywords = that.form.keywords.trim();
            ns.post({
                action: 'getaddressbook',
                Keywords: that.form.keywords,
                DepartmentID: that.CompanyID,
            }, function (succeed, data, err) {
                if (succeed) {
                    let resultData = data.data;
                    if (data.status && resultData) {
                        that.ProjectList = resultData;
                        that.FullName = that.ProjectList[0].Name;
                        that.CompanyID = that.ProjectList[0].ID;
                        that.get_data();
                        var newHeaderList = [];
                        newHeaderList.push(that.ProjectList[0]);
                        that.headerlist = newHeaderList;
                        that.list = resultData;
                        setTimeout(function () {
                            that.topHeader = document.getElementById("chooseproject").clientHeight + 76;
                        }, 100);
                    };
                }
            });
        },
        get_data: function () {
            var that = this;
            that.list = [];
            that.form.keywords = that.form.keywords.trim();
            ns.post({
                action: 'getaddressbook',
                Keywords: that.form.keywords,
                DepartmentID: that.CompanyID,
            }, function (succeed, data, err) {
                if (succeed) {
                    let resultData = data.data;
                    if (data.status && resultData) {
                        that.list = resultData;
                        if (that.chooseList.length > 0 && that.list.length > 0) {
                            that.list.forEach((item, index) => {
                                item.isselected = false;
                                let result = that.chooseList.findIndex((val) => {
                                    return val.UserID == item.UserID;
                                });
                                if (result > -1) {
                                    item.isselected = true;
                                }
                            });
                        }
                    };
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
            }, {
                toast: true,
            });
        },
        do_open: function (item, type) {
            var that = this;
            if (item.ID == that.CompanyID || item.IsMan) {
                return;
            }
            that.CompanyID = item.ID;
            var findIndex = that.headerlist.findIndex((val) => {
                return item.ID === val.ID;
            });
            if (type == 1) {
                var newlist = that.headerlist.slice(0, findIndex + 1);
                that.headerlist = newlist;
            }
            else {
                if (findIndex > -1) {
                    return;
                }
                that.headerlist.push(item);
            }
            setTimeout(function () {
                that.topHeader = document.getElementById("chooseproject").clientHeight + 76;
            }, 100);
            that.get_data();
        },
        onReturnOne(val) {
            var returnvalue = "空";
            let textValue = val.trim();
            if (textValue != "") {
                returnvalue = textValue.slice(-1);
            }
            return returnvalue;
        },
        do_search: function () {
            this.get_data();
        },
        doHideSearch: function () {
            var that = this;
            that.showchoose = false;
            that.showsearch = false;
        },
        showChooseAll: function () {
            var that = this;
            that.showchoose = !that.showchoose;
            that.showsearch = !that.showsearch;//底部阴影
        },
        doChoosUser: function (item) {
            var that = this;
            if (item.UserID == undefined || item.UserID <= 0 || item.UserID == null) {
                return;
            }
            //单选
            if (!that.IsMultiple) {
                that.chooseList = [];
                that.chooseList.push(item);
            }
            //多选
            else {
                item.isselected = !item.isselected;
                if (item.isselected) {
                    that.chooseList.push(item);
                }
                else {
                    const index = that.chooseList.findIndex(p => p.UserID === item.UserID);
                    that.chooseList.splice(index, 1)
                }
                const new_list = [...new Set(that.chooseList)];// 去重
                that.chooseList = new_list;
            }
        },
        set_select_items: function () {
            var that = this;
            that.select_id_list = [];
            for (var i = 0; i < that.chooseList.length; i++) {
                var item = that.chooseList[i];
                that.select_id_list.push(item.UserID);
            }
        },
        do_save(){
            var that = this;
            that.set_select_items();
            if (that.select_id_list.length == 0) {
                api.toast({
                    msg: '请选择人员',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }

            //维修工单
            var options = {};
            options.action = "deviceservicepublic";
            options.ID = that.ID;
            options.ServiceType = that.ServiceType;
            options.RefreshPublic = that.PublishType==2?true:false;//是否是-重新指派
            options.HandleUserIDs = JSON.stringify(that.select_id_list);
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        api.toast({
                            msg: '操作成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        api.sendEvent({
                            name: 'reloadrepairlist',
                        });
                        api.sendEvent({
                            name: 'updata_repair_con',
                        });
                        setTimeout(function () {
                            api.closeWin();
                        }, 1000)
                        return;
                    }
                    api.toast({
                        msg: data.Msg == undefined ? '操作失败' : data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                    return          
                } else if (err) {
                    api.toast({
                        msg: err == undefined ? '接口失败' : err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
            return;
        },
        do_save_one: function () {
            var that = this;
            api.sendEvent({
                name: 'updata_choose_user',
                extra: {
                    data: that.chooseList,
                }
            });
            setTimeout(function () {
                api.closeWin();
            }, 500)
        },
        doChooseOne(){
            var that = this;
            api.sendEvent({
                name: 'choose_user_one',
                extra: {
                    data: that.chooseList,
                }
            });
            setTimeout(function () {
                api.closeWin();
            }, 500)
        },
        doChooseMore(){
            var that = this;
            api.sendEvent({
                name: 'choose_user_more',
                extra: {
                    data: that.chooseList,
                }
            });
            setTimeout(function () {
                api.closeWin();
            }, 500)
        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.ID = api.pageParam.ID || 0;
    app.IsMultiple = api.pageParam.IsMultiple || false;
    app.PublishType = api.pageParam.type || 0;
    app.ServiceType = api.pageParam.ServiceType || 1;
    app.IsChoose = api.pageParam.IsChoose || false;
    ns.init_search();
    app.get_project();
};
